package com.winner;

/**
 * 思路:可以利用wait和notify方法实现，一个线程打印，打印完就释放对象锁，唤醒另一个线程打印，打印完释放对象锁，唤醒另一个线程打印，交替执行
 */
public class ThreadJiaoTiDaYin {

    private static final Object lock = new Object();//锁

    public static void main(String[] args) {
            new Thread(() -> {
                for (int i = 1; i <= 5; i++) {
                    synchronized (lock) {
                        lock.notify();//唤醒，获取锁
                        System.out.print(i);
                        try {
                            lock.wait();//打印之后等待,释放锁，t2拿到锁开始工作
                        } catch (Exception e) {
                            e.getLocalizedMessage();
                        }
                    }
                }
            }).start();
            new Thread(() -> {
                for (int i = 97; i < 102; i++) {
                    synchronized (lock) {
                        lock.notify();
                        System.out.print((char) i);
                        try {
                            lock.wait(); //打印之后等待
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
    }
}

